home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Mine Sweeper 1.1 / Source / event.c < prev    next >
Text File  |  1996-01-04  |  8KB  |  451 lines

  1. /*    event.c
  2.  *
  3.  *        The event manager routines
  4.  */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include "event.h"
  9. #include "mines.h"
  10.  
  11. /****************************************************************/
  12. /*                                                                */
  13. /*                            Forwards                            */
  14. /*                                                                */
  15. /****************************************************************/
  16.  
  17. int GetEvent(EventRecord *);            /* Fetch Event Stuff    */
  18. void DoMenu(long);                        /* Process events        */
  19. void DoMouse(EventRecord *);
  20. void DoAboutMe(void);                    /* Put up about me box    */
  21.  
  22. /****************************************************************/
  23. /*                                                                */
  24. /*                            Globals                                */
  25. /*                                                                */
  26. /****************************************************************/
  27.  
  28. short        quitFlag;                    /* Event Quit Flag        */
  29.  
  30. short        SizeX = 30;                    /* size bitmaps            */
  31. short        SizeY = 15;
  32. short        SizeFlag = 2;                /* Easy mode            */
  33. short        Bombs = 90;                    /* # of bombs            */
  34. short        BombFlag = 1;                /* Easy mode (again)    */
  35. short        BRatio = 5;                    /* Bomb ratio            */
  36.  
  37. short        BStatus[40][40];            /* Bomb status            */
  38. short        BPosition[40][40];            /* Bomb info flags        */
  39. short        BState = 0;                    /* Game state            */
  40. short        BFirst = 1;                    /* First click?            */
  41. short        BCount = 0;                    /* Bomb count value        */
  42.  
  43. long        LogTime;
  44. long        CurTime;
  45.  
  46. short        XLoc,YLoc;                    /* X, Y location        */
  47.  
  48. ScoreRec    Scores[6];                    /* High scores            */
  49.  
  50. WindowPtr    MineWindow = NULL;            /* Mines window            */
  51.  
  52. short        Cruse = 0;                    /* Cruse Control        */
  53.  
  54. /****************************************************************/
  55. /*                                                                */
  56. /*    Window Dispatcher Routines                                    */
  57. /*                                                                */
  58. /****************************************************************/
  59.  
  60. /*    DoClose
  61.  *
  62.  *        Handle closing this window
  63.  */
  64.  
  65. void DoClose(WindowPtr w)
  66. {
  67.     short i;
  68.     
  69.     if (w == NULL) return;
  70.     
  71.     i = ((WindowPeek)w)->windowKind;
  72.     if (i < 0) CloseDeskAcc(i);
  73.     else switch (i) {
  74.         case WK_GAME:
  75.             quitFlag = 1;
  76.             break;
  77.         default:
  78.             CloseWindow(w);
  79.             break;
  80.     }
  81. }
  82.  
  83. /*    DoUpdate
  84.  *
  85.  *        This handles the update event
  86.  */
  87.  
  88. void DoUpdate(WindowPtr w)
  89. {
  90.     short i = ((WindowPeek)w)->windowKind;
  91.     
  92.     BeginUpdate(w);
  93.     SetPort(w);
  94.     switch (i) {
  95.         case WK_GAME:
  96.             UpdateMines(w);
  97.             break;
  98.         default:
  99.             break;
  100.     }
  101.     EndUpdate(w);
  102. }
  103.  
  104. /*    DoClick
  105.  *
  106.  *        Handle mouse down
  107.  */
  108.  
  109. void DoClick(WindowPtr w, Point pt, short mods)
  110. {
  111.     short i = ((WindowPeek)w)->windowKind;
  112.     
  113.     SetPort(w);
  114.     GlobalToLocal(&pt);
  115.     switch (i) {
  116.         case WK_GAME:
  117.             ClickMines(w,pt,mods);
  118.             break;
  119.     }
  120. }
  121.  
  122.  
  123.  
  124. /****************************************************************/
  125. /*                                                                */
  126. /*                        Event Dispatcher                        */
  127. /*                                                                */
  128. /****************************************************************/
  129.  
  130. /*    DoEvent
  131.  *
  132.  *        Get the next event and process it
  133.  */
  134.  
  135. int DoEvent()
  136. {
  137.     EventRecord e;
  138.     char c;
  139.     
  140.     quitFlag = 0;                        /* Clear the quit status flag */
  141.         
  142.     /*
  143.      *    Handle the next event in the event queue
  144.      */
  145.     
  146.     DoPeriodic();
  147.     SystemTask();
  148.     GetNextEvent(everyEvent,&e);
  149.     switch (e.what) {
  150.         case mouseDown:                    /* Handle mouse down events */
  151.             DoMouse(&e);
  152.             break;
  153.         case keyDown:                    /* Handle keyboard events */
  154.         case autoKey:
  155.             c = e.message & charCodeMask;
  156.             if (e.modifiers & cmdKey) {
  157.                 if (e.what == keyDown) DoMenu(MenuKey(c));
  158.             }
  159.             break;
  160.         case updateEvt:                    /* Handle window update events */
  161.             DoUpdate((WindowPtr)(e.message));
  162.             break;
  163.         case activateEvt:                /* Handle window activate/deactivate events */
  164.             break;
  165.     }
  166.     
  167.     return quitFlag;                    /* Return the quit status flag */
  168. }
  169.  
  170.  
  171. /****************************************************************/
  172. /*                                                                */
  173. /*                        Mouse Dispatcher                        */
  174. /*                                                                */
  175. /****************************************************************/
  176.  
  177. /*    DoMouse
  178.  *
  179.  *        This does the right thing when the mouse button is
  180.  *        clicked
  181.  */
  182.  
  183. static void DoMouse(event)
  184. EventRecord *event;
  185. {
  186.     int i;
  187.     WindowPtr w;
  188.     GrafPtr foo;
  189.     Rect r;
  190.     
  191.     i = FindWindow(event->where,&w);
  192.     if (w != NULL) SetPort(w);                                /* Assure current port */
  193.     
  194.     switch (i) {
  195.         case inDesk:
  196.             break;
  197.         case inMenuBar:
  198.             DoMenu(MenuSelect(event->where));
  199.             break;
  200.         case inSysWindow:
  201.             SystemClick(event,w);
  202.             break;
  203.         case inDrag:
  204.             GetWMgrPort(&foo);
  205.             r = foo->portRect;
  206.             InsetRect(&r,4,4);
  207.             r.top += 20;
  208.             DragWindow(w,event->where,&r);
  209.             break;
  210.         case inGoAway:
  211.             if (w != NULL) {
  212.                 if (TrackGoAway(w,event->where)) DoClose(w);
  213.             }
  214.             break;
  215.         case inContent:
  216.             DoClick(w,event->where,event->modifiers);
  217.             break;
  218.     }
  219. }
  220.  
  221. /****************************************************************/
  222. /*                                                                */
  223. /*                        Menu Dispatcher                            */
  224. /*                                                                */
  225. /****************************************************************/
  226.  
  227.  
  228.  
  229. /*    DoMenu
  230.  *
  231.  *    How to dispatch menu events
  232.  */
  233.  
  234. static void DoMenu(l)
  235. long l;
  236. {
  237.     short hi,lo;
  238.     unsigned char buffer[255];
  239.     MenuHandle mh;
  240.         
  241.     hi = (short)(l >> 16);
  242.     lo = (short)(l & 0x0FFFF);
  243.         
  244.     switch (hi) {
  245.         case APPLMENU:
  246.             switch (lo) {
  247.                 case ABOUTME:
  248.                     DoAboutMe();
  249.                     break;
  250.                 default:
  251.                     mh = GetMHandle(APPLMENU);
  252.                     if (mh != NULL) {
  253.                         GetItem(mh,lo,buffer);
  254.                         OpenDeskAcc(buffer);
  255.                     }
  256.                     break;
  257.             }
  258.             break;
  259.         case EDITMENU:
  260.             SystemEdit(lo-1);
  261.             break;
  262.         case FILEMENU:
  263.             switch (lo) {
  264.                 case QUIT:                /* Quit application */
  265.                     quitFlag = 1;
  266.                     return;
  267.                 case CLOSE:
  268.                     DoClose(FrontWindow());
  269.                     break;
  270.                 case NEW:
  271.                     NewMines();
  272.                     break;
  273.                 case PREF:
  274.                     Preferences();
  275.                     break;
  276.             }
  277.     }
  278.     HiliteMenu(0);
  279. }
  280.  
  281.  
  282. /****************************************************************/
  283. /*                                                                */
  284. /*                        About Me Display                        */
  285. /*                                                                */
  286. /****************************************************************/
  287.  
  288.  
  289. /*    CenterWindow
  290.  *
  291.  *        Move this window to make it in the center of the main
  292.  *    screen
  293.  */
  294.  
  295. static void CenterWindow(w)
  296. WindowPtr w;
  297. {
  298.     GrafPtr foo;
  299.     Rect r;
  300.     short x,y;
  301.     
  302.     r = w->portRect;
  303.     x = r.right - r.left;
  304.     y = r.bottom - r.top;
  305.     GetWMgrPort(&foo);
  306.     r = foo->portRect;
  307.     x = r.right - r.left - x;
  308.     y = r.bottom - r.top - y - 22;
  309.     x /= 2;
  310.     y /= 2;
  311.     y += 22;
  312.     
  313.     MoveWindow(w,x,y,1);
  314. }
  315.  
  316.  
  317. /*    AboutMeDH
  318.  *
  319.  *    Handle the ModalDialog for the 'about me' dialog
  320.  */
  321.  
  322. pascal Boolean _AboutMeDH(DialogPtr theDialog,EventRecord *theEvent,short *itemHit)
  323. {
  324. #    pragma unused(theDialog)
  325.  
  326.     if (theEvent->what == mouseDown) {
  327.         *itemHit = 1;
  328.         return true;
  329.     } else return false;
  330. }
  331.  
  332. /*    BufferScore
  333.  *
  334.  *        Put into a buffer the score
  335.  */
  336.  
  337. void BufferScore(char *b, short i)
  338. {
  339.     short n;
  340.     
  341.     if (Scores[i].time >= 1000) {
  342.         b[0] = '\0';
  343.         return;
  344.     }
  345.     NumToString(Scores[i].time,(unsigned char *)b);
  346.     while (b[0] < 5) b[++b[0]] = ' ';
  347.     for (n = 1; n <= Scores[i].name[0]; n++) b[++b[0]] = Scores[i].name[n];
  348. }
  349.  
  350. /*    DoHelp
  351.  *
  352.  *        Display help dialogs
  353.  */
  354.  
  355. static void DoHelp(void)
  356. {
  357.     DialogPtr dlog;
  358.     short i;
  359.     
  360.     dlog = GetNewDialog(130,NULL,(WindowPtr)-1);
  361.     if (dlog == NULL) return;
  362.     SetPort(dlog);
  363.     CenterWindow(dlog);
  364.     ShowWindow(dlog);
  365.  
  366.     for (;;) {
  367.         ModalDialog((ModalFilterUPP)_AboutMeDH,&i);
  368.         if (i == 1) break;
  369.     }
  370.  
  371.     DisposDialog(dlog);
  372. }
  373.  
  374.  
  375. /*    DoAboutMe
  376.  *
  377.  *    How to handle the 'about me' box
  378.  */
  379.  
  380. static void DoAboutMe()
  381. {
  382.     DialogPtr dlog;
  383.     short i1;
  384.     Rect r;
  385.     Handle i2;
  386.     char buffer[48];
  387.     
  388.     /*
  389.      *    About me...
  390.      */
  391.     
  392.     dlog = GetNewDialog(128,NULL,(WindowPtr)-1);
  393.     if (dlog == NULL) return;
  394.     SetPort(dlog);
  395.     CenterWindow(dlog);
  396.     ShowWindow(dlog);
  397.     GetDItem(dlog,1,&i1,&i2,&r);
  398.     InsetRect(&r,-4,-4);
  399.     PenSize(3,3);
  400.     FrameRoundRect(&r,16,16);
  401.     PenSize(1,1);
  402.     
  403.     /*
  404.      *    Draw high scores
  405.      */
  406.     
  407.     GetDItem(dlog,4,&i1,&i2,&r);
  408.     FrameRect(&r);
  409.     TextFont(1);
  410.     TextSize(9);
  411.     
  412.     MoveTo(r.left + 5, r.top + 12);
  413.     DrawString("\pHigh Scores for Large Field/High Density:");
  414.     TextFont(4);
  415.     MoveTo(r.left + 10, r.top + 26);
  416.     TextFace(4);
  417.     DrawString("\pTime Name");
  418.     TextFace(0);
  419.     for (i1 = 0; i1 < 6; i1++) {
  420.         MoveTo(r.left + 10, r.top + 36 + 10 * i1);
  421.         BufferScore(buffer,i1);
  422.         DrawString((unsigned char *)buffer);
  423.     }
  424.     
  425.     TextFont(0);
  426.     TextSize(12);
  427.     
  428.     for (;;) {
  429.         ModalDialog(NULL,&i1);
  430.         if ((i1 == 1) || (i1 == 2)) break;
  431.     }
  432.     DisposDialog(dlog);
  433.     if (i1 == 1) return;
  434.     if (i1 == 2) DoHelp();
  435. }
  436.  
  437.  
  438. /****************************************************************/
  439. /*                                                                */
  440. /*    Main entry point                                            */
  441. /*                                                                */
  442. /****************************************************************/
  443.  
  444. main()
  445. {
  446.     InitApp();                        /* Start me up                */
  447.     NewMines();
  448.     while (!DoEvent());
  449.     EndApp();                        /* Finish me off            */
  450. }
  451.